import React from 'react';
import numeral from 'numeral';
import { DonutChart } from 'bizcharts';
type PropField = {
  datas: Record<string, any>[];
  xCol: string;
  yCol: string;
  height: number;
  label?: string;
};
const PieChart: React.FC<PropField> = (props) => {
  const { datas, xCol, yCol, height } = props;
  const label = props.label ? props.label : '日均';
  if (!datas) {
    return null;
  }
  return (
    <DonutChart
      data={datas}
      title={{
        visible: false,
      }}
      meta={{
        [xCol]: {
          alias: '类别',
        },
        [yCol]: {
          alias: label,
          formatter: (v) => {
            return `${v}%`;
          },
        },
      }}
      autoFit
      height={height}
      radius={0.8}
      padding="auto"
      angleField={yCol}
      colorField={xCol}
      statistic={{
        title: {
          formatter: () => label,
        },
        content: {
          formatter: (_, data) => {
            let sumVal = 0;
            data!.map((item) => {
              sumVal = sumVal + item.val;
            });
            return numeral(sumVal).format('0,0');
          },
        },
      }}
    />
  );
};
export default PieChart;
